package ar.edu.itba.twiteros.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import ar.edu.itba.twiteros.util.Facebook;
import ar.edu.itba.twiteros.util.GrantTwitterAccess;

import com.google.code.facebookapi.Attachment;
import com.google.code.facebookapi.AttachmentMediaImage;
import com.google.code.facebookapi.FacebookException;
import com.google.code.facebookapi.FacebookParam;

@SuppressWarnings("serial")
public class FeedServlet extends HttpServlet{

	@Override
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		
	}
	
	@Override
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		RequestDispatcher dispacher= getServletContext().getRequestDispatcher("/index");

		Long uid= Long.valueOf(req.getParameter("fb_sig_user"));
		Facebook facebook= new Facebook(req.getParameter(FacebookParam.SESSION_KEY.toString()));
		Twitter twitter = new Twitter();
		GrantTwitterAccess grantAccess= new GrantTwitterAccess(twitter, uid);
		
		Long status_id=null;
		Object obj= req.getParameter("status_id");
		if(obj==null || (status_id= Long.valueOf((String)obj))==null ){
			req.setAttribute("error_message", "Invalid id number of twitter message");
			dispacher= getServletContext().getRequestDispatcher("/error_msg.jsp");
			dispacher.forward(req, resp);
		}
		
		try {
			if(grantAccess.setAccessToken())
			{
				Status status;
		//		status = twitter.showStatus(status_id);
				List<Status> statuses=twitter.getFriendsTimeline();
				status= getStatus(statuses, status_id);
				
				if(status!=null){
					Attachment attachment=  new Attachment();
					attachment.setName(status.getUser().getName());
					attachment.setHref(status.getUser().getURL().toString());
					attachment.setDescription(status.getText());
					AttachmentMediaImage attImage= new AttachmentMediaImage();
					attImage.addImage(status.getUser().getProfileImageURL().toString(), status.getUser().getURL().toString());
					attachment.setMedia(attImage);
					
					
					facebook.getClient().stream_publish("shared a twitter message", attachment, null, uid, uid);
					
					req.setAttribute("shared", true);
					req.setAttribute("shared_message",status.getText());
				}
				
			}
		} catch (FacebookException e) {
			req.setAttribute("error_message", e.getMessage());
			dispacher= getServletContext().getRequestDispatcher("/error.jsp");
			dispacher.forward(req, resp);
		}catch (TwitterException e2) {
			req.setAttribute("error_message", e2.getMessage());
			dispacher= getServletContext().getRequestDispatcher("/error.jsp");
			dispacher.forward(req, resp);
		}
		
		dispacher.forward(req, resp);
	}

	
	public Status getStatus(List<Status> statuses, long id){
		Status status=null;
		
		for (Status s : statuses) {
			if(id==s.getId()){
				status=s;
				break;
			}
		}
		
		return status;
	}
}